<?php /** 
 * PHP5 MySQL Database Class implementing the Singleton Design Pattern
 * An object is a singleton if the application can include one and only one of that object at a time
 * Usage:  
 *  $single = MySQLi::call();      
 *   $result = $single->query("SELECT * FROM ...");  
 */
class MySqliDb 
{ 
    
	private static $instance; // stores the MySQLi instance 
    private static $obj;  
    private function __construct() { } // block directly instantiating   
    private function __clone() { } // block cloning of the object    
/**
  * Creates and references the mysqli object.  
  * @access public  
  * @return object mysqli database object
  * 
  **/   
   public static function call($server,$username,$password,$db)   
   {   
       // create the instance if it does not exist   
       if(!isset(self::$instance))   
       {   
           // the MYSQL_* constants should be set to or   
           //  replaced with your db connection details   
           self::$instance = new mysqli($server,$username,$password,$db);   
           if(self::$instance->connect_error)   
           {   
                throw new Exception('MySQL connection failed: ' . self::$instance->connect_error);   
           }  
           mysqli_query(self::$instance,"SET NAMES 'utf8'");         
        }   
        // return the instance   
        return self::$instance;   
   }
   ///////////////////////////////////////////////////////
 /**  
* Creates and references the MysqliDb object.  
* @access public  
* @return object MysqliDb object  
*/ 
public static function instance($server,$username,$password,$db)
{ 
       if (self::$instance)  
           self::$obj = new MysqliDb();  
        else {
            self::$instance = self::call($server,$username,$password,$db);
            self::$obj= new MySqliDb();
        }
        return self::$obj;  
} 
/**  
* Creates and references the MysqliDb object.  
* @access public  
* @return object MysqliDb object  
*/ 
public static function instance1($server,$username,$password,$db)
{ 
	$objects = array();
    // if (self::$instance) {
        // self::$obj = new MysqliDb();
		// $objects['obj'] = self::$obj;
		// $objects['db'] = self::$instance;
	// }	else {
        // self::$instance = self::call($server,$username,$password,$db);
        // self::$obj = new MySqliDb();
		// $objects['obj'] = self::$obj;
		// $objects['db'] = self::$instance;
    // }
		// the MYSQL_* constants should be set to or   
           //  replaced with your db connection details   
           self::$instance = new mysqli($server,$username,$password,$db);   
           if(self::$instance->connect_error)   
           {   
                throw new Exception('MySQL connection failed: ' . self::$instance->connect_error);   
           }  
        mysqli_query(self::$instance,"SET NAMES 'utf8'"); 
        self::$obj = new MySqliDb();
		$objects['obj'] = self::$obj;
		$objects['db'] = self::$instance;
        return $objects; 
}      
///////////////////////////////////////////////////////
public function query($sql)
{
    $result = mysqli_query(self::$instance,$sql);
    if($result){
		$rows_num = mysqli_num_rows($result);        
        if(isset($rows_num) && $rows_num !=0 ){  /// multi row results
            while($row = mysqli_fetch_assoc($result)){
                $rows[]=$row; 
            }
            return $rows;
        }
        else
           return false;
    }
} 
public function query1($ins,$sql)
{
    $result = mysqli_query($ins,$sql);
    if($result){
		$rows_num = mysqli_num_rows($result);        
        if(isset($rows_num) && $rows_num !=0 ){  /// multi row results
            while($row = mysqli_fetch_assoc($result)){
                $rows[]=$row; 
            }
            return $rows;
        }
        else
           return false;
    }
}   
   
        
} 


?>